import { ElMessageBox, ElCascaderPanel } from 'element-plus'
import { h } from 'vue'
import { selectTenantApi } from '/@/api/system/auth'
import { setTenantId } from '/@/utils/auth'



export async function useTenantSelect(loginData: any) {

  const { succeeded, data } = await selectTenantApi(loginData)

  if (!data.length) throw new Error('登录失败');

  // if (data.length === 1) {
  //   setTenantId(data[0].id)
  //   return;
  // }

  let selectTenantId;
  var res = await ElMessageBox({
    title: '选择租户',
    message: h(ElCascaderPanel, {
      class: ['select-tenant'],
      options: data.map(s => ({ label: s.name, value: s.id })),
      onChange(val) {
        selectTenantId = val[0]
      },
    })
  })

  if (res !== 'confirm') {
    throw Error('取消登录')
  }

  setTenantId(selectTenantId)
}
